/*百度地图离线API V2 接口文件
 *网页中只需要加载此JS文件即可
 *百度地图图片即瓦片文件请自行下载
 */
var bdmapcfg = {
    'imgext': '.png', //瓦片图的后缀 ------ 根据需要修改，一般是 .png .jpg
    'tiles_dir': '/baiduOffLineMap/tiles/'   //瓦片图的目录，为空默认在 api/tiles/ 目录
};

//////////////////下面的保持不动///////////////////////////////////
var scripts = document.getElementsByTagName("script");
var JS__FILE__ = scripts[scripts.length - 1].getAttribute("src");
bdmapcfg.home = JS__FILE__.substr(0, JS__FILE__.lastIndexOf("/") + 1); //地图API主目录, images,tiles都在此目录下
(function () {
    window.BMap_loadScriptTime = (new Date).getTime();
    //加载地图css文件
    document.write('<link rel="stylesheet" type="text/css" href="' + bdmapcfg.home + 'css/baidu_map_v2.css"/>');
    //加载地图配置信息文件
    document.write('<script type="text/javascript" src="' + bdmapcfg.home + 'baiduTilesInfo.js"></script>');
    //加载地图API主文件
    document.write('<script type="text/javascript" src="' + bdmapcfg.home + 'baidumap_api_v2.0_min.js"></script>');
})();

/*
全局配置参数列表
*/
var APIPATH = "/baiduOffLineMap/"; //api部署的服务器地址 最后包含/哦
var APIIMAGESFILENAME = "images"; //api中图标文件存放的文件夹名称
var APIOVERLAYFILENAME = "tiles"; // api离线瓦片图存放的文件夹名称
var APIOVERLATYIMAGETYPE = "png"; //api离线瓦片图图片格式
/*如果浏览器不兼容，请过滤掉不兼容的浏览器 
默认有： MSIE=IE7-IE9, Windows=IE10或以上, Firefox=火狐, Chrome=360, Opera=欧朋, Safari=苹果
*/
var APIBROWSERTYPE = ["MSIE", "Windows", "Chrome"]; //需要过滤的就手动加载进来 请注意大小写哦

/*
   存储离线地图图片集合
*/
var MapImagePath = [];

////显示鹰眼  o= 地图控件ID
var imgpathstop = "";
var isOverView = false;//是否显示了鹰眼

/**
* 动态加载js脚本文件
* url：js文件的地址路径
* callback：文件加载完成执行的函数
*/
function loadJsFile(url, callback) {
    var script = document.createElement("script")
    script.type = "text/javascript";
    if (script.readyState) {  //IE
        script.onreadystatechange = function () {
            if (script.readyState == "loaded" || script.readyState == "complete") {
                script.onreadystatechange = null;
                if (callback) {
                    callback();
                }
            }
        };
    } else {  //Others
        script.onload = function () {
            if (callback) {
                callback();
            }
        };
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

/**
* 动态加载css样式文件
* url:css文件的地址路径
* callback：文件加载完成后执行的函数
*/
function loadCssFile(url, callback) {
    var css = document.createElement("link")
    css.type = "text/css";
    if (css.readyState) {  //IE
        css.onreadystatechange = function () {
            if (css.readyState == "loaded" || css.readyState == "complete") {
                css.onreadystatechange = null;
                if (callback) {
                    callback();
                }
            }
        };
    } else {  //Others
        css.onload = function () {
            if (callback) {
                callback();
            }
        };
    }
    css.href = url;
    css.rel = "stylesheet";
    document.getElementsByTagName("head")[0].appendChild(css);
}

/**
* 初始化地图
*/
function initMap(divName) {
    // 百度地图API功能
    map = new BMap.Map(divName, { mapType: getDefaultMapType() });
    //设置加载瓦片图
    loadOverlayImage(map);
    //设置显示缩放控件
    showControl(map);
    //启动鼠标拖动
    showZoom(map);
    //添加比例尺
    showScale(map, BMAP_ANCHOR_TOP_LEFT);
    //设置默认的中心点及放大级别
    initCenterAndZoom(map, centX, centY, 12);
    return map;
}

/**
*初始化到中心点 LNG,LAT坐标位置  Zoom默认显示级别
*/
function initCenterAndZoom(map, lng, lat, Zoom) {
    var point = new BMap.Point(lng, lat);  //设置地图中心点经度纬度
    map.centerAndZoom(point, Zoom);
}

/**
* 默认地图类型 必须选择项 minZooms=地图显示最小级别  maxZooms地图显示最大级别
*/
function getDefaultMapType(minZooms, maxZooms) {
    // 离线瓦片
    if (minZooms == undefined) { minZooms = minLevel; }
    if (maxZooms == undefined) { maxZooms = maxLevel; }
    var tileLayer = new BMap.TileLayer();
    tileLayer.getTilesUrl = function (tileCoord, zoom) {
        var x = tileCoord.x;
        var y = tileCoord.y;
        var url = APIPATH + APIOVERLAYFILENAME + '/' + (zoom) + "/" + x + "/" + y + "." + APIOVERLATYIMAGETYPE; //直接使用本地瓦片
        return url;
    }
    var myType = new BMap.MapType("MyMap", tileLayer, {
        minZoom: minZooms,
        maxZoom: maxZooms
    });
    var weixing = BMAP_HYBRID_MAP;
    return myType;
}

/**
* 加载离线瓦片图
*/
function loadOverlayImage(map) {
    var overlayTileLayer = new BMap.TileLayer({ isTransparentPng: true });
    overlayTileLayer.getTilesUrl = function (tileCoord, zoom) {
        var x = tileCoord.x;
        var y = tileCoord.y;
        //var gx = toGoogleX(x,zoom);//谷歌地图转换
        //var gy = toGoogleY(y,zoom);//谷歌地图转换
        //var url = APIPATH + APIOVERLAYFILENAME + '/' + (zoom) + "/" + gx + "/" + gy + "." + APIOVERLATYIMAGETYPE; //直接使用本地瓦片,根据当前坐标，选取合适的瓦片图,不建议此方法,会导致拖动的时候卡顿,建议直接使用百度瓦片
        var url = APIPATH + APIOVERLAYFILENAME + '/' + (zoom) + "/" + x + "/" + y + "." + APIOVERLATYIMAGETYPE; //直接使用本地瓦片
        //document.getElementById("info_div").innerHTML+=url+"<br/>";                 
        if (isOverView) { MapImagePath.push(url); }
        return url;
    }
    map.addTileLayer(overlayTileLayer);
}

/**
* 显示缩放控件
*/
function showControl(map, type) {
    //添加缩放控件
    var types = BMAP_ANCHOR_TOP_LEFT;
    if (type != undefined) { types = BMAP_ANCHOR_TOP_RIGHT; }
    map.addControl(new BMap.NavigationControl({
        anchor: types,
        type: BMAP_NAVIGATION_CONTROL_LARGE
    }));
}

/**
* 获取地图标注的经纬度信息
*/
function getPoint(marker) {
    if (marker != null) {
        return point = marker.point;
    }
    return null;
}

/**
* 获取红色定位图标
*/
function getRedMarkerIcon() {
    return new BMap.Icon("../api/images/marker_red_sprite.png", new BMap.Size(18, 24));
}

/**
* 获取黑色定位图标
*/
function getBlackMarkerIcon() {
    return new BMap.Icon("../api/images/marker_black_sprite.png", new BMap.Size(18, 24));
}

/**
* 获取红色星星图标
*/
function getRedStarIcon() {
    return new BMap.Icon("../api/images/star_red.png", new BMap.Size(24, 24));
}


/**
* 显示比例尺
* map=地图对象   type=0左上角 1右上角 2左下角 3右下角 不带值默认0
*/
function showScale(map, type) {
    if (type == undefined) { type = 0; }
    var showType = BMAP_ANCHOR_TOP_LEFT;
    if (type == 1) { showType = BMAP_ANCHOR_TOP_RIGHT; }
    if (type == 2) { showType = BMAP_ANCHOR_BOTTOM_LEFT; }
    if (type == 3) { showType = BMAP_ANCHOR_BOTTOM_RIGHT; }
    var top_left_control = new BMap.ScaleControl({ anchor: showType }); // 左上角，添加比例尺
    map.addControl(top_left_control);
}

function showOverview(map, mapId) {
    isOverView = true;
    var overViewOpen = new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT });
    map.addControl(overViewOpen);
    if (mapId) {
        if (document.getElementById(mapId).onmousemove == null) {
            document.getElementById(mapId).onmousemove = function () {
                if (MapImagePath.length > 0) {
                    imgpathstop = MapImagePath[0];
                }
                //alert(getElementsByClassName('BMap_omMapContainer').innerHTML);
                //BMap_omMapContainer  BMap_omViewMask
                try { getElementsByClassName('BMap_omMapContainer').innerHTML = "<img src='" + imgpathstop + "'/>"; } catch (e) { }
                MapImagePath.length = 0;
            }
        }
    }
}

/**
* 获取class的对象
*/
function getElementsByClassName(n) {
    var classElements = [], allElements = document.getElementsByTagName('*');
    for (var i = 0; i < allElements.length; i++) {
        if (allElements[i].className == n) {
            classElements[classElements.length] = allElements[i];
        }
    }
    return classElements[0];
}


/**
* 显示鼠标放大缩小
*/
function showZoom(map) {
    map.enableContinuousZoom(); //放大缩小样式
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    map.enableScrollWheelZoom(); //启用滚轮放大缩小
    //启用键盘操作
    map.enableKeyboard();
}

/**
* 地图缩小放大事件
*/
function zoomEvent(map, callFun) {
    map.addEventListener('zoomend', function (e) {
        callFun(e);
    });
}

/**
* 得到地图缩放级别
*/
function getZoom(map) {
    return map.getZoom();
}

/**
* 得到地图中心点
*/
function getCenter(map) {
    var ct = map.getCenter();
    return ct.lng + "," + ct.lat;
}

/**
* 重置缩放级别和中心点
*/
function resetZoomAndCenter(map, zoom, lng, lat) {
    map.setZoom(zoom);
    map.setCenter(new BMap.Point(lng, lat));
}


/**
* 添加标注点  lng,lat=经纬度坐标 ico=标注显示的图片 icoWidth=标注图片宽度 icoHeight=标注图片的高度  text=显示的提示文本，borderColor=文本边框的颜色 fontColor=文本颜色 fontSize=字体大小
*/
function setMarker(map, lng, lat, ico, icoWidth, icoHeight, Text, borderColor, fontColor, fontSizes) {
    var point = new BMap.Point(lng, lat);
    var MarkerList = []; ///把添加的标注放到集合中去
    var marker;
    if (ico != undefined && ico != "") {
        var icons = new BMap.Icon(ico, new BMap.Size(icoWidth, icoHeight));
        marker = new BMap.Marker(point, { icon: icons }); //初始化地图标记
    } else {
        marker = new BMap.Marker(point); //初始化地图标记
    }
    map.addOverlay(marker); //将标记添加到地图中
    MarkerList.push(marker);
    if (Text != undefined && Text != "") {
        var opts = {
            position: point,    // 指定文本标注所在的地理位置
            offset: new BMap.Size(10, -30)    //设置文本偏移量
        }
        var label = new BMap.Label("&nbsp;" + Text + "&nbsp;", opts);  // 创建文本标注对象
        label.setStyle({
            color: fontColor,
            border: "1px solid " + borderColor,
            fontSize: fontSizes + "px",
            height: "20px",
            lineHeight: "20px",
            fontFamily: "微软雅黑"
        });
        map.addOverlay(label);
        MarkerList.push(label);
    }
    return MarkerList;
}


/**
* 弹出坐标点提示信息窗体
*/
function openMapWindow(map, content, point, opts) {
    //窗体标题
    var winTitle = "<hr style='border:0.6px solid gray; margin-top:0px;margin-bottom:8px;' />"
    content = "<div>" + winTitle + content + "</div>";
    var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对
    map.openInfoWindow(infoWindow, point);
}

/**
* 关闭地图弹出的图层
*/
function closeMapWindow(map) {
    if (map != null) {
        map.closeInfoWindow();
    }
}

/**
* 开启画区域
*/
function drawing(map, drawingStyle, callFun) {
    //实例化鼠标绘制工具
    var drawingManager = new BMapLib.DrawingManager(map, drawingStyle);
    //添加鼠标绘制工具监听事件，用于获取绘制结果
    drawingManager.addEventListener('overlaycomplete', callFun);
    return drawingManager;
}

/**
* 锁定地图  isLoad=true锁定 false取消锁定
*/
function lockMap(map, isLoad) {
    if (isLoad) {
        map.disableDragging();
    } else {
        map.enableDragging();
    }
}



/*
 谷歌瓦片图转换
*/
var baiduX = new Array(0, 0, 1, 3, 6, 12, 24, 49, 98, 197, 395, 790, 1581, 3163, 6327, 12654, 25308, 50617);
var baiduY = new Array(0, 0, 0, 1, 2, 4, 9, 18, 36, 73, 147, 294, 589, 1178, 2356, 4712, 9425, 18851);
var googleX = new Array(0, 1, 3, 7, 13, 26, 52, 106, 212, 425, 851, 1702, 3405, 6811, 13623, 27246, 54492, 107917);
var googleY = new Array(0, 0, 1, 2, 5, 12, 23, 47, 95, 190, 380, 761, 1522, 3045, 6091, 12183, 24366, 47261);

/*
  谷歌地图瓦片图转换
*/
function toGoogleX(x, z) {
    var b = baiduX[z - 1]; //395
    var g = googleX[z - 1]; //11:843,12:1685
    var gx = g + (x - b); //   --- 1587+
    //谷歌瓦片行编号=[谷歌参照瓦片行编号+(百度行编号 – 百度参照瓦片行编号)]
    return gx;
}
function toGoogleY(y, z) {
    var b = baiduY[z - 1]; //147
    var g = googleY[z - 1]; //10:
    var gy = g - (y - b); //
    //谷歌瓦片列编号=[谷歌参照瓦片列编号- (百度列编号 – 百度参照瓦片列编号)] //向上，列为递减
    //alert(y);
    return gy;
}


function obj2string(o) {
    var r = [];
    if (typeof o == "string") {
        return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    }
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o) {
                r.push(i + ":" + obj2string(o[i]));
            }
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}";
        } else {
            for (var i = 0; i < o.length; i++) {
                r.push(obj2string(o[i]))
            }
            r = "[" + r.join() + "]";
        }
        return r;
    }
    return o.toString();
}
